using System;
using System.Linq;
using System.Xml.Linq;
using Ayatta.Contract;
using FluentValidation;
using FluentValidation.Results;
using System.Collections.Generic;
using Ayatta.Contract.Component;

namespace Ayatta.Contract.Domain
{
    /// <summary>
    /// 商品
    /// </summary>
    [Serializable]
    public class Product : Versionable<int>
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 副名称
        /// </summary>
        public string SubName { get; set; }

        /// <summary>
        /// 商品编码
        /// </summary>
        public string ProductCode { get; set; }

        /// <summary>
        /// 分类编码
        /// </summary>
        public string CategoryCode { get; set; }

        /// <summary>
        /// 简单描述
        /// </summary>
        public string ShortDescription { get; set; }

        /// <summary>
        /// 市场价格
        /// </summary>
        public decimal OriginalPrice { get; set; }

        /// <summary>
        /// 商城价格
        /// </summary>
        public decimal UnitPrice { get; set; }

        /// <summary>
        /// 管理员备注
        /// </summary>
        public string AdminMemo { get; set; }

        /// <summary>
        /// 商品属性(xml)
        /// </summary>
        public string XmlAttribute { get; set; }

        /// <summary>
        /// 商品销售属性(xml)
        /// </summary>
        public string SaleXmlAttribute { get; set; }

        /// <summary>
        /// 商品维度(以销售属性计算)
        /// </summary>
        public ProductDimension Dimension { get; set; }

        /// <summary>
        /// 显示位置
        /// </summary>
        public ShowOnPosition ShowOnPosition { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public ProductItemStatus Status { get; set; }

        public string Message { get; set; }
        #region 只读属性
        /// <summary>
        /// 商品属性
        /// </summary>
        public virtual IList<ProductAttributeInfo> ProductAttributesInfo
        {
            get
            {
                IList<ProductAttributeInfo> info = new List<ProductAttributeInfo>();
                if (!string.IsNullOrEmpty(XmlAttribute))
                {
                    try
                    {
                        var xElement = XElement.Parse(XmlAttribute);

                        info = xElement.Elements("attribute").Select(o => new ProductAttributeInfo {
                            NameCode = o.Attribute("nameCode").Value,
                            Name = o.Attribute("name").Value,
                            ValueCode = o.Attribute("valueCode").Value,
                            Value = o.Attribute("value").Value,
                        }).ToList();
                    }
                    catch (Exception)
                    {
                        Message = "解析属性出错";
                    }
                }
                return info;
            }
        }

        /// <summary>
        /// 商品销售属性
        /// </summary>
        public virtual IList<ProductSaleAttributeInfo> ProductSaleAttributesInfo
        {
            get
            {
                var info = new List<ProductSaleAttributeInfo>();
                if (!string.IsNullOrEmpty(SaleXmlAttribute))
                {
                    try
                    {
                        var xElement = XElement.Parse(SaleXmlAttribute);

                        info = xElement.Elements("attribute").Select(o => new ProductSaleAttributeInfo {
                            Code = o.Attribute("code").Value,
                            Text = o.Attribute("text").Value,
                            Values = o.Elements("value").ToDictionary(v => v.Attribute("code").Value, v => v.Attribute("text").Value)
                        }).ToList();
                    }
                    catch (Exception)
                    {
                        Message = "解析属性出错";
                    }
                }
                return info;
            }
        }
        #endregion

        #region 导航属性
        /// <summary>
        /// 商品扩展
        /// </summary>
        public virtual ProductExtra ProductExtra { get; set; }

        /// <summary>
        /// 商品变体
        /// </summary>
        public virtual IEnumerable<ProductVariant> ProductVariants { get; set; }

        /// <summary>
        /// 商品属性
        /// </summary>
        public virtual IEnumerable<ProductAttribute> ProductAttributes { get; set; }

        #endregion

        protected override ValidationResult Validate()
        {
            var validator = new Validator<Product>();
            validator.RuleFor(o => o.Name).NotEmpty().WithMessage("物品名不能为空");

            return validator.Validate(this);
        }
    }

}